﻿using System;
using System.Collections.Generic;
using DMS.DTOs;
using DMS.Services;
using DMS.Web.Messages;

namespace DMS.Web.DataSources
{
    public class CustomerDataSource
    {
        private readonly ICustomerService service = ServiceLocatorHelper.GetService<ICustomerService>();

		public IList<CustomerDTO> Select(string country)
		{
			try
			{
				ResultListDTO<CustomerDTO> result = service.GetCustomersByContry(country);

				if (result.Messages != null)
					foreach (string message in result.Messages)
						MessageManager.SetMessage(new Message(message, MessageSeverity.Error));

				return result.Items;
			}
			catch (Exception ex)
			{
				MessageManager.SetMessage(new Message(ex));
				return new List<CustomerDTO>();
			}
		}

    	public void Save(CustomerDTO dto)
		{
			try
			{
				service.Save(dto);
			}
			catch (Exception ex)
			{
				MessageManager.SetMessage(new Message(ex));
			}
		}

    	public void Delete(CustomerDTO dto)
        {
			try
			{
				service.DeleteById(dto.Id);
			}
			catch(Exception ex)
			{
				MessageManager.SetMessage(new Message(ex));
			}
        }
    }
}
